//
//  XWSetttingItemCell.m
//  CGWSWW
//
//  Created by lanou3g on 16/8/13.
//  Copyright © 2016年 CXG. All rights reserved.
//

#import "XWSetttingItemCell.h"
#import "XWSettingItem.h"
#import "XWSettingArrowItem.h"
#import "XWSettingLabelItem.h"
#import "XWSettingSwitchItem.h"
#define rowH 30
#define width self.bounds.size.width
@interface XWSetttingItemCell ()
@property(nonatomic,strong)XWSettingLabelItem *labelItem;
@property(nonatomic,strong)XWSettingSwitchItem *switchItem;
/**
 *  箭头
 */
@property (nonatomic, strong) UIImageView *arrowView;
/**
 *  开关
 */
@property (nonatomic, strong) UISwitch *switchView;
/**
 *  标签
 */
@property (nonatomic, strong) UILabel *labelView;

@end
@implementation XWSetttingItemCell
-(XWSettingSwitchItem*)switchItem{
    if (!_switchItem) {
        _switchItem = [[XWSettingSwitchItem alloc]init];
    }
    return _switchItem;
}
-(XWSettingLabelItem*)labelItem{
    if (!_labelItem) {
        _labelItem = [[XWSettingLabelItem alloc]init];
    }
    return _labelItem;
}
- (UIImageView *)arrowView
{
    if (_arrowView == nil) {
        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"youbian"]];
    }
    return _arrowView;
}

//- (UISwitch *)switchView
//{
//    if (_switchView == nil) {
//        _switchView = [[UISwitch alloc] init];
//    }
//    return _switchView;
//}

- (UILabel *)labelView
{
    if (_labelView == nil) {
        _labelView = [[UILabel alloc] init];
        _labelView.bounds = CGRectMake(0, 0, 100, 30);
        _labelView.textColor = [UIColor orangeColor];
        [_labelView setTextAlignment:(NSTextAlignmentRight)];
//        _labelView.backgroundColor =[UIColor redColor];
    }
    return _labelView;
}
//在这里对cell加上标签 在UITableView上直接调用这个类方法就可以直接取出cell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉下面的分割线
    static NSString *ID = @"setting";
    XWSetttingItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[XWSetttingItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

- (void)setItem:(XWSettingItem *)item
{
    _item = item;
    
    // 1.设置数据
    [self setupData];
    
    // 2.设置右边的内容
    [self setupRightContent];
}

/**
 *  设置右边的内容
 */
//根据目标控制器的继承类 进行条件判断
- (void)setupRightContent
{
    if ([self.item isKindOfClass:[XWSettingArrowItem class]]) { // 箭头
        self.accessoryView = self.arrowView;
    } else if ([self.item isKindOfClass:[XWSettingSwitchItem class]]) { // 开关
        self.switchItem = (XWSettingSwitchItem*)self.item;
        
        self.switchView = self.switchItem.settingSwitch;
        
         self.accessoryView = self.switchView;
//
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    } else if ([self.item isKindOfClass:[XWSettingLabelItem class]]) { // 标签
        self.accessoryView = self.labelView;
       self.labelItem = (XWSettingLabelItem*)self.item;
        self.labelView.text = self.labelItem.accessoryLabelView_text;
    } else {
        //以防从重用池取出cell 是前面判断的cell
        self.accessoryView = nil;
    }
 
}

/**
 *  设置数据
 */
- (void)setupData
{
    if (self.item.icon.length) {
        self.imageView.image = [UIImage imageNamed:self.item.icon];
    }
    self.textLabel.text = self.item.title;
}

@end
